<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 11/11/14
 * Time: 10:48 AM
 */

class Contest extends Eloquent
{

    public function childs()
    {
        return $this->hasMany('Contest', 'parent_id', 'id');
    }

    public function parent()
    {
        return $this->belongsTo('Contest', 'parent_id', 'id');
    }

    public function getContest($id)
    {
        $key = 'contest-' . $id;
        $contest = null;
        if (Cache::has($key))
            $contest = Cache::get($key);
        else {
            $contest = $this->find($id);
            Cache::put($key, $contest, 180);
        }
        return $contest;
    }

    public function getContestNav($id)
    {
        $key = 'contest-nav-' . $id;
        $contestNav = array();
        if (Cache::has($key))
            $contestNav = Cache::get($key);
        else {
            $contestNav = $this->find($id)->childs()->get();
            Cache::put($key, $contestNav, 180);
        }
        return $contestNav;
    }
} 